package com.tsc.dateinfo.data;
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.SocketException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Iterator;

import com.tsc.dateinfo.DatabaseHelper;
import com.tsc.dateinfo.ServerLog;


public class ParseCouponMoa implements IDataParser{
	
	public static final String STR_URL="http://www.couponmoa.com/product/popPageingProductList.do";
	//public static final String STR_URL_FRONT="http://www.couponmoa.com/index_chart_pop.asp?sp=&ar=&ca=1&page=";
	//public static final String STR_URL_BACK="&ed=&td=&s1=&s2=&s3=&s4=&ca2=&ov=&fdata=&csearch=&sca=&ta=E&SortOv=&cStart=";
	//public static final String STR_URL_FRONT="http://www.couponmoa.com/product/popPageingProduct.do?menuCid=E&cCate1=&cCate2=&cCate3=&cSido=&searchAreas=&storesNationwide=&curPageNo=";
	//public static final String STR_URL_BACK="&orderbyGubun=&searchGubun=&searchDate=&listType=1&outShopTypeCodes=&shopCode=&shopDealType=&interestYn=N";
	//public static final String STR_DETAIL_URL_FRONT = "http://m.couponmoa.com/sub/coupon_view.asp?mobile=yes&fg=&wPart=MO&ta=&Pseq=";
	//public static final String STR_DETAIL_URL_BACK = "&cpv=";
	
	public static final String STR_DETAIL_URL_FRONT = "http://m.couponmoa.com/detail/detail.do?did=";
	public static final String STR_DETAIL_URL_BACK = "&todayYn=";
	//http://m.couponmoa.com/detail/detail.do?did=MjEzODExNQ&todayYn=
	
	
	
	public static void main(String[] agrs)
	{
		ParseCouponMoa moa = new ParseCouponMoa();
		moa.parse();
		ArrayList<IData> data = moa.getData();
		Iterator<IData> iter = data.iterator();
		
		while(iter.hasNext()) {
			DataCouponMoa d = (DataCouponMoa)iter.next();
			System.out.println("=============================");
			System.out.println(d.testStr());
			
		}
		System.out.println("=============================");
		System.out.println(""+data.size());
	}

	private String strCode;
	private ArrayList<IData> mData = null;
	private ArrayList<IData> newAddedData = null;
	private int mTotalItem = 0;
	private int mTotalPage = 0;
	private DatabaseHelper mDbHelper = null;
	
	public ParseCouponMoa()
	{
		mData = new ArrayList<IData>();
		newAddedData = new ArrayList<IData>();
	}
	
	public ArrayList<IData> getData()
	{
		return mData;
	}
	
	public void parse(CouponMoaArea area) {
		
		//mData = new ArrayList<IData>();
		int page = 1;
		
		ArrayList<IData> listData;
		
		boolean oneMoreTry = false;
		page=1;		
		int numSocketException = 0;
		int numItemsInArea = 0;
		while(true)
		{
			try {
				getHtmlCode(page,area.sido,area.area);
				
			}catch(SocketException e)
			{
				numSocketException++;
				try {
					Thread.sleep(10000);
				} catch (InterruptedException e1) {
					e1.printStackTrace();
				}
				if(numSocketException > 5)
				{
					page++;
					continue;
				}
				else {
					continue;
				}
			}
			
			numSocketException = 0;
			int itemNumber = getItemNum();				
			

			if(itemNumber != 50)
			{
				try {
					FileWriter fw = new FileWriter("page.html");
					fw.write(strCode);
					fw.close();
				}
				catch(Exception e){}
				
				if(oneMoreTry) {
					listData = makeData(area);
					numItemsInArea += listData.size();
					mData.addAll(listData);
					ServerLog.log("info", "get "+listData.size()+"items in "+area.sidoName + " " 
							+ area.areaName + " " + page+"page");
					
					ServerLog.log("info", "break");
					break;
				}
				else {
					oneMoreTry = true;
					page--;
					
					ServerLog.log("info", "fail. one more try");
					
					try {
						Thread.sleep(3000);
					}catch(InterruptedException ex) {}
				}
				
			}
			else {
				listData = makeData(area);
				numItemsInArea += listData.size();
				mData.addAll(listData);
				ServerLog.log("info", "get "+listData.size()+"items in "+area.sidoName + " " 
						+ area.areaName + " " + page+"page");
				
				oneMoreTry = false;
			}
			page++;
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		ServerLog.log("info", "total " + numItemsInArea + "items found in " + area.sidoName + " " + area.areaName);
		ServerLog.log("info", "total " + mData.size() + "items found");
		
		
		Iterator<IData> iter = mData.iterator();
		ServerLog.log("info", "get detail. total " + mData.size() +"items"+"\r\n");
		int num = 1;
		numSocketException = 0;
		while(iter.hasNext())
		{
			DataCouponMoa d = (DataCouponMoa)iter.next();
			try {
				getDetail(d);
			} catch (SocketException e1) {
				// TODO Auto-generated catch block
				numSocketException++;
				if(numSocketException >5) {
					numSocketException = 0;
					continue;					
				}
				else
				{
				}
			}
			ServerLog.log("info","["+ num + "/"+mData.size()+"]: "+d.getTitle()+"\n");
			num++;
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
	}
	
	public void parse() {	
		
		
		int i;
		for(i=0;i<CouponMoaArea.AREA_INFO.length;i++)
		{
			mData.clear();
			System.gc();
			CouponMoaArea area = CouponMoaArea.AREA_INFO[i];
			parse(area);
			mDbHelper.deleteCouponMoa(area);
			mDbHelper.insertCouponMoa(mData);
		}	
		
		
	}
	
	
	
	public void setDBHelper(DatabaseHelper dbHelper)
	{
		mDbHelper = dbHelper;
	}
	
	
	private void getHtmlCode(int pageNumber, String sido, String areas) throws SocketException {
		try {
			URL url = new URL(STR_URL);
			URLConnection conn = url.openConnection();
			conn.setConnectTimeout(60000);
			conn.setReadTimeout(60000);
			
			conn.setRequestProperty("Accept", "text/html, */*; q=0.01");
			conn.setRequestProperty("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
			conn.setRequestProperty("X-Requested-With", "XMLHttpRequest");
			conn.setRequestProperty("Accept-Language", "ko");
			conn.setRequestProperty("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)");
			conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
			conn.setRequestProperty("Host", "www.couponmoa.com");
			conn.setRequestProperty("Content-Length", "189");
			conn.setRequestProperty("DNT", "1");
			conn.setRequestProperty("Connection","Keep-Alive");
			conn.setRequestProperty("Cache-Control", "no-cache");
			conn.setDoOutput(true);
			conn.setDoInput(true);
					
			
			OutputStream os = conn.getOutputStream();
			String strPost = "menuCid=E&cCate1=&cCate2=&cCate3=&cSido="+sido+"&searchAreas="+areas+"&storesNationwide=&curPageNo="+
				pageNumber+"&orderbyGubun=&searchGubun=&searchDate=&listType=1&outShopTypeCodes=&shopCode=&shopDealType=&interestYn=N";
			os.write(strPost.getBytes());
			os.flush();
			os.close();
			
			InputStream is = conn.getInputStream();
			InputStreamReader isr = new InputStreamReader(is, "UTF-8");
			
			BufferedReader br = new BufferedReader(isr);
			String strLine;
			StringBuffer strBuf = new StringBuffer();
			while((strLine = br.readLine()) != null) {
				String str = new String(strLine.getBytes(), "UTF-8");
				
				
				strBuf.append(str);				
				strBuf.append("\n");
			}
			
			this.strCode = strBuf.toString();
			
			FileWriter fw = new FileWriter("pageCode.html");
			fw.write(strCode);
			fw.close();
			

		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	
	private ArrayList<IData> makeData(CouponMoaArea area)
	{
		int s = 0;
		int e = 0;
		
		ArrayList<IData> listData = new ArrayList<IData>();
		
		
		
		//상품 정보 있는 부분 찾기
		while((s = strCode.indexOf("<span class=\"edge\">", e))!= -1)
		{
			DataCouponMoa data = new DataCouponMoa();
			
			s = strCode.indexOf("<img src=\"", s);
			s += "<img src=\"".length();
			e = strCode.indexOf("\"", s);
			data.setImgUrl(strCode.substring(s,e));
			
			s = strCode.indexOf("<div class=\"product_tit\">", e);
			s = strCode.indexOf("dealClick('", s);
			s+= "dealClick('".length();
			e = strCode.indexOf("'",s);
			data.setItemId(strCode.substring(s,e));
			
			s = strCode.indexOf(">", e);
			s += 1;
			e = strCode.indexOf("</a>", s);
			data.setTitle(strCode.substring(s,e));
			
			int temp;
			int temp1;
			s = strCode.indexOf("<span class=\"sale_price\">", e);
			temp = strCode.indexOf("<strong class=\"en\">", s);
			temp1 = strCode.indexOf("<strong class=\"kor\">",s);
			if(temp1 == -1)
			{
				s = temp;
				s = strCode.indexOf("<strong class=\"en\">", s);
				s += "<strong class=\"en\">".length();
				e = strCode.indexOf("</strong>",s);
				String price = strCode.substring(s,e);
				price = price.replace(",","");
				data.setDiscPrice(Integer.parseInt(price));
			}
			else if(temp == -1)
			{
				s = temp1;
				data.setDiscPrice(-1);
				s = strCode.indexOf("<div class=\"discountSmall\">", s);
				s = strCode.indexOf("<strong class=\"", s);
				s = strCode.indexOf(">", s);
				s +=1;
				e = strCode.indexOf("</strong>", s);
				data.setAidInfo(strCode.substring(s,e));
			}
			
			/*
			s = strCode.indexOf("<a class=\"category_location\">", e);
			s += "<a class=\"category_location\">".length();
			e = strCode.indexOf("</a>",s);
			String strArea = strCode.substring(s,e);
			strArea = strArea.replace("\n", "");
			strArea = strArea.replace("\t", "");
			strArea = strArea.replace("\r", "");
			*/
			data.setArea(area.areaName);
			
			listData.add(data);
			
		}
		
		return listData;
	}
	
	
	public int getItemNum()
	{
		int s;
		s = 0;
		int itemNum = 0;;
		while(true)
		{
			s = strCode.indexOf("<div class=\"product_tit\">",s );
			if(s == -1)
				break;
			else
				s++;
			itemNum++;
		}
		
		return itemNum;
	}
	
	public void getDetail(DataCouponMoa data) throws SocketException
	{
		String strDetailCode = "";
		
		try {
			URL url = new URL(STR_DETAIL_URL_FRONT + data.getItemId() + STR_DETAIL_URL_BACK);
			URLConnection conn = url.openConnection();
			
			conn.setConnectTimeout(60000);
			conn.setReadTimeout(60000);
			
			//conn.connect();
			conn.setRequestProperty("Accept", "text/html, application/xhtml+xml, */*");
			conn.setRequestProperty("Accept-Language", "ko");
			conn.setRequestProperty("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)");
			conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
			conn.setRequestProperty("Host", "m.couponmoa.com");
			conn.setRequestProperty("DNT", "1");
			conn.setRequestProperty("Connection","Keep-Alive");
			conn.setDoOutput(true);
			conn.setDoInput(true);
			
			conn.connect();
			
			OutputStream os = conn.getOutputStream();
			os.flush();
			os.close();
			
			InputStream is = conn.getInputStream();
			InputStreamReader isr = new InputStreamReader(is, "UTF-8");
			
			BufferedReader br = new BufferedReader(isr);
			String strLine;
			StringBuffer strBuf = new StringBuffer();
			while((strLine = br.readLine()) != null) {
				String str = new String(strLine.getBytes(), "UTF-8");
				
				
				strBuf.append(str);				
				strBuf.append("\n");
			}
			
			strDetailCode = strBuf.toString();
			FileWriter fw = new FileWriter("test.html");
			fw.write(strDetailCode);
			fw.close();
			
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		
		int s = 0;
		int e = 0;
		
		
		
		
		s = strDetailCode.indexOf("<div class=\"contents\">");
		s = strDetailCode.indexOf("dealClick('",s);
		s += "dealClick('".length();
		e = strDetailCode.indexOf("'", s);
		s = strDetailCode.indexOf("'", e+1);
		s+=1;
		e = strDetailCode.indexOf("'", s);
		String urlScid = strDetailCode.substring(s,e);
		s = strDetailCode.indexOf("'", e+1);
		s+=1;
		e = strDetailCode.indexOf("'", s);
		String urlCid = strDetailCode.substring(s,e);
		s = strDetailCode.indexOf("'", e+1);
		s+=1;
		e = strDetailCode.indexOf("'", s);
		String urlSid = strDetailCode.substring(s,e);
		s = strDetailCode.indexOf("'", e+1);
		s+=1;
		e = strDetailCode.indexOf("'", s);
		String urlAids = strDetailCode.substring(s,e);
		s = strDetailCode.indexOf("'", e+1);
		s+=1;
		e = strDetailCode.indexOf("'", s);
		String urlCurRank = strDetailCode.substring(s,e);
		s = strDetailCode.indexOf("'", e+1);
		s+=1;
		e = strDetailCode.indexOf("'", s);
		String urlTodayYn = strDetailCode.substring(s,e);
		String url = "http://m.couponmoa.com/main/goPage.do?data="
				+ data.getItemId()
				+ "&scid="+ urlScid
				+ "&cid=" + urlCid
				+ "&sid=" + urlSid
				+ "&aids=" + urlAids
				+ "&curRank=" + urlCurRank
				+ "&todayYn=" + urlTodayYn;
		data.setUrl(url);
		s = strDetailCode.indexOf("<p class=\"cost\">", e);
		s = strDetailCode.indexOf("<span class=\"del\">", s);
		s += "<span class=\"del\">".length();
		e = strDetailCode.indexOf("</span>", s);
		String price = strDetailCode.substring(s,e);
		price = price.replace(",", "");
		data.setOrigPrice(Integer.parseInt(price));
			
		s = strDetailCode.indexOf("<div class=\"item_detail\">", e);
		s = strDetailCode.indexOf("<dt>내용</dt>", s);
		s = strDetailCode.indexOf("<dd>",s);
		s += "<dd>".length();
		e = strDetailCode.indexOf("</dd>", s);
		data.setText(strDetailCode.substring(s,e));
		
		s = strDetailCode.indexOf("<dt>사용가능일</dt>", e);
		if(s != -1)
		{
			s = strDetailCode.indexOf("<dd>", s);	
			s += "<dd>".length();
			e = strDetailCode.indexOf(" ", s);
			data.setStartDate(strDetailCode.substring(s,e));
			s = strDetailCode.indexOf("~", e);
			s += 1;
			e = strDetailCode.indexOf(" ", s);
			data.setEndDate(strDetailCode.substring(s,e));
			
		}
		
		
		ArrayList<Float> listLatitude = new ArrayList<Float>();
		ArrayList<Float> listLongitude = new ArrayList<Float>();
		s = 0;
		e = 0;
		try {
			while(true) {
				s = strDetailCode.indexOf("beaches.push(['",s);
				if(s == -1)
					break;
				s += "beaches.push(['".length();
				e = strDetailCode.indexOf("'", s);
				s = strDetailCode.indexOf("'", e+1);
				s+=1;
				e = strDetailCode.indexOf("'", s);
				listLatitude.add(Float.parseFloat(strDetailCode.substring(s,e)));
				s = strDetailCode.indexOf("'", e+1);
				s+=1;
				e = strDetailCode.indexOf("'", s);
				listLongitude.add(Float.parseFloat(strDetailCode.substring(s,e)));
				
			}
		}
		catch(Exception ex){
			return;
		}
		try {
			data.setLatitude(listLatitude.get(0));
			data.setLongitude(listLongitude.get(0));
		}
		catch(Exception ex){
			return;
		}
		
		for(int i=1;i<listLatitude.size();i++){
			DataCouponMoa copyData = data.copyNewInstance();
			copyData.setLatitude(listLatitude.get(i));
			copyData.setLongitude(listLongitude.get(i));
			newAddedData.add(copyData);
		}
		
		
		
		
		
	}
	
	
}